<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">

      <div>
        <!-- 搜索 -->
        <el-input v-model="query.ddbm" clearable size="small" placeholder="机构编码1" style="width: 170px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.xm" clearable size="small" placeholder="姓名" style="width: 170px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.sfz" clearable size="small" placeholder="身份证" style="width: 170px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.zt" filterable clearable size="small" placeholder="照护状态" class="filter-item" @change="crud.toQuery">
          <el-option
            v-for="item in dict.jgzhzt"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select v-model="query.sndj" filterable clearable size="small" placeholder="失能等级" class="filter-item" @change="crud.toQuery">
          <el-option
            v-for="item in dict.sndj"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select v-model="query.bisZhsqCblx" filterable clearable size="small" placeholder="参保类型" class="filter-item" @change="crud.toQuery">
          <el-option
            v-for="item in dict.cblx"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="660px">
        <el-form ref="form" :inline="true" :model="form" size="small" label-width="110px">
          <el-form-item label="姓名" prop="bisZhsqSnryXm">
            <el-input v-model="form.bisZhsqSnryXm" disabled style="width: 170px;" />
          </el-form-item>
          <el-form-item label="身份证" prop="bisZhsqSnrySfz">
            <el-input v-model="form.bisZhsqSnrySfz" disabled style="width: 170px;" />
          </el-form-item>
          <el-form-item label="性别" prop="bisZhsqSnryXb">
            <el-input v-model="form.bisZhsqSnryXb" disabled style="width: 170px;" />
          </el-form-item>
          <el-form-item label="年龄" prop="bisZhsqNl">
            <el-input v-model="form.bisZhsqNl" disabled style="width: 170px;" />
          </el-form-item>
          <el-form-item label="医保卡号" prop="bisZhsqYbkh">
            <el-input v-model="form.bisZhsqYbkh" disabled style="width: 170px;" />
          </el-form-item>
          <el-form-item label="参保状态" prop="bisZhsqCbzt">
            <el-input v-model="form.bisZhsqCbzt" disabled style="width: 170px;" />
          </el-form-item>
          <el-form-item label="待遇开始日期" prop="bisZhsqDykkrq">
            <el-input v-model="form.bisZhsqDykkrq" disabled style="width: 170px;" />
          </el-form-item>
          <el-form-item label="待遇结束日期" prop="bisZhsqDyjsrq">
            <el-input v-model="form.bisZhsqDyjsrq" disabled style="width: 170px;" />
          </el-form-item>
          <el-form-item label="医保所属区县" prop="bisZhsqSnryYbssqxCode">
            <el-select v-model="form.bisZhsqSnryYbssqxCode" disabled filterable placeholder="请选择" style="width: 170px;">
              <el-option
                v-for="item in dict.ccstcqh"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="ADL量表评分" prop="bisZhsqAdl">
            <el-input v-model="form.bisZhsqAdl" readonly style="width: 170px;" disabled>
              <el-button slot="append" icon="el-icon-document" title="测量ADL量表" />
            </el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="bisZhsqLxdh">
            <el-input v-model="form.bisZhsqLxdh" disabled style="width: 170px;" />
          </el-form-item>
          <el-form-item label="地址" prop="bisZhsqHldz">
            <el-input v-model="form.bisZhsqHldz" disabled style="width: 170px;" />
          </el-form-item>
          <el-form-item label="监护人姓名" prop="bisZhsqJhrxm">
            <el-input v-model="form.bisZhsqJhrxm" disabled style="width: 170px;" />
          </el-form-item>
          <el-form-item label="监护人电话" prop="bisZhsqJhrdh">
            <el-input v-model="form.bisZhsqJhrdh" disabled style="width: 170px;" />
          </el-form-item>
          <el-form-item label="关系" prop="bisZhsqGx">
            <el-select v-model="form.bisZhsqGx" disabled filterable placeholder="请选择" style="width: 170px;">
              <el-option
                v-for="item in dict.yjsgx"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="申请类别" prop="bisZhsqSqlb">
            <el-select v-model="form.bisZhsqSqlb" disabled filterable placeholder="请选择" style="width: 170px;">
              <el-option
                v-for="item in dict.sqlb"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="提交日期" prop="bisZhsqTjsj">
            <el-date-picker v-model="form.bisZhsqTjsj" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd" placeholder="调整的日期" style="width: 170px;" />
          </el-form-item>
          <el-form-item label="申请原因">
            <el-input v-model="form.bisZhsqSqyy" disabled :rows="3" type="textarea" style="width: 470px;" />
          </el-form-item>
          <span class="demo-image__preview">
            <el-form-item label="失能老人资料">
              <div v-for="(item, index) in tempFiles" :key="index">
                <a :href="item.path2" target="_blank" style="color:blue;font-size: 12px;">
                  <span v-if="item.fjlb === 'orgAdlLiveVideo'">ADL视频</span>
                </a>
              </div>
            </el-form-item>
          </span>
          <span v-if="roleId === 547" class="demo-image__preview">
            <el-image
              v-for="item in files2"
              :key="item"
              style="width: 100px; height: 100px"
              :src="item"
              :preview-src-list="files2"
            />
          </span>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="info" @click="zhHistoryQuery()">查看意见</el-button>
          <el-button v-if="view === 0" :loading="crud.status.cu === 2" type="primary" @click="doSave">保存</el-button>
          <el-button @click="crud.cancelCU">关 闭</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column align="center" prop="bisZhsqZt" label="申请状态" width="70">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.bisZhsqZt === '05'" effect="plain" type="">
              {{ dict.label.zhzt[scope.row.bisZhsqZt] }}
            </el-tag>
            <el-tag v-else-if="scope.row.bisZhsqZt === '00'" effect="plain" type="warning">
              {{ dict.label.zhzt[scope.row.bisZhsqZt] }}
            </el-tag>
            <el-tag v-else effect="plain" type="danger">
              {{ dict.label.zhzt[scope.row.bisZhsqZt] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="lrsj" label="申请时间" width="150" />
        <el-table-column align="center" prop="bisZhsqYbkh" label="医保卡号" width="90" />
        <el-table-column align="center" prop="bisZhsqSnryXm" label="姓名" width="70" />
        <el-table-column align="center" prop="bisZhsqSnryXb" label="性别" width="50" />
        <el-table-column align="center" prop="bisZhsqSnrySfz" label="身份证" width="150" />
        <el-table-column align="center" prop="bisZhsqCblx" label="参保类型" width="90" />
        <el-table-column align="center" prop="bisZhsqNl" label="年龄" width="50" />
        <el-table-column align="center" prop="bisZhsqDykkrq" label="待遇开始日期" width="100" />
        <el-table-column align="center" prop="bisZhsqDyjsrq" label="待遇结束日期" width="100" />
        <el-table-column align="center" prop="bisZhsqSndj" label="失能等级" />
        <el-table-column align="center" prop="bisZhsqAdl" label="评分" width="50" />
        <el-table-column align="center" prop="bisZhsqSnryYbssqxName" label="统筹区" width="90" />
        <el-table-column align="left" header-align="center" prop="bisSbgsQymc" label="商保机构" width="250" />
        <el-table-column align="left" header-align="center" prop="bisPggsQymc" label="评估机构" width="250" />
        <el-table-column align="center" prop="bisZhsqHljgBm" label="定点编码" width="90" />
        <el-table-column align="left" header-align="center" prop="bisHljgQymc" label="护理机构" width="250" />
        <el-table-column align="center" prop="pgry" label="评估人员" />
        <el-table-column align="center" prop="pgsj" label="评估时间" width="150" />
        <el-table-column align="center" prop="fhry" label="复核人员" />
        <el-table-column align="center" prop="fhsj" label="复核时间" width="150" />
        <el-table-column
          align="center"
          fixed="right"
          label="操作"
          width="300"
        >
          <template slot-scope="scope">
            <!--            <el-button size="mini" type="success" icon="el-icon-notebook-2" @click="handleScore(scope.row)">查看评分</el-button>-->
            <el-button size="mini" type="success" icon="el-icon-document" @click="handleConclusion(scope.row)">查看评估结论</el-button>
            <el-button size="mini" type="success" icon="el-icon-view" @click="handleClick(scope.row)">查看</el-button>
            <el-button :loading="crud.status.cu === 2" size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <el-dialog :visible.sync="dialogYjFormVisible" width="720px">
        <el-table ref="yjtable" v-loading="zhHistoryDataloading" :data="zhHistoryData">
          <el-table-column type="index" :index="indexMethod" label="序号" align="center" />
          <el-table-column prop="lrsj" label="操作时间" align="center" />
          <el-table-column prop="lrrxm" label="操作人" align="center" />
          <el-table-column prop="type" label="操作类型" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.type === '02'" effect="plain" type="success">
                评估
              </el-tag>
              <el-tag v-else-if="scope.row.type === '04'" effect="plain" type="primary">
                复核
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="result" label="操作结果" align="center" />
          <el-table-column prop="bz" label="意见" align="center" width="200px" />
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogYjFormVisible = false">关 闭</el-button>
        </div>
      </el-dialog>
      <!-- 评估结论页面 -->
      <indexConclusion ref="indexConclusion" />
      <!-- 评估结论页面 -->
      <indexScore ref="indexScore" />
    </div>
  </div>
</template>

<script>
import crudZhsq from '@/api/business/zhsq'
import crudFile from '@/api/tools/localStorage'
import indexConclusion from './indexConclusion'
import indexScore from './indexScore'
import CRUD, { presenter, header, form } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import moment from 'moment'

const defaultForm = { bisZhsqId: null, bisZhsqHljgId: null, bisZhsqSnryId: null, bisZhsqSnryXm: null, bisZhsqSnryXb: null, bisZhsqSnrySfz: null, bisZhsqDykkrq: null, bisZhsqYbkh: null, bisZhsqCbzt: null, bisZhsqDyjsrq: null, bisZhsqXsdyq: null, bisZhsqAdl: null, bisZhsqZt: null, bisZhsqSnryYbssqx: null, ywlx: null, lrr: null, lrrxm: null, lrbm: null, lrbmmc: null, lrsj: null, gxr: null, gxrxm: null, gxbm: null, gxbmmc: null, gxsj: null, bz: null, bisZhsqAdlPc: null, bisZhsqSndj: null, bisZhsqRybh: null, bisZhsqNl: null, bisZhsqLxdh: null, bisZhsqHldzSsqx: null, bisZhsqHldzXxdz: null, bisZhsqHldz: null, bisZhsqJhrxm: null, bisZhsqJhrdh: null, bisZhsqGx: null, bisZhsqSqyy: null, bisZhsqGrbh: null, adlZhsqs: [], bisZhsqCblx: null, bisZhsqSqlb: null, bisZhsqTjsj: null }
export default {
  name: 'Zhsq',
  components: { pagination, rrOperation, indexConclusion, indexScore },
  mixins: [presenter(), header(),
    form(function() {
      return Object.assign({ hljg: { bisHljgId: this.bisHljgId }}, defaultForm)
    })],
  dicts: ['xb', 'yjsgx', 'ccs', 'zhzt', 'ccstcqh', 'jgzhzt', 'sndj', 'cblx'],
  cruds() {
    return CRUD({ title: '居家照护申请', url: 'api/generalQuery/centerQuerOrgZhsqxxIn220101', query: { bisHljgId: '' }, idField: 'bisZhsqId', sort: 'lrsj,desc', crudMethod: { ...crudZhsq }})
  },
  data() {
    return {
      dialogYjFormVisible: false,
      zhHistoryDataloading: false,
      zhHistoryData: [],
      adlDrawer: false,
      approveDialog: false,
      selectedZhids: [],
      bisHljgId: '',
      files: [],
      tempFiles: [],
      adlData: [],
      roleId: 0,
      view: 0,
      permission: {
        add: ['admin', 'zhsq:add'],
        edit: ['admin', 'zhsq:edit'],
        del: ['admin', 'zhsq:del']
      }
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    /**
     * 点击修改之前校验
     * @param crud
     * @param form
     * @returns {boolean}
     */
    [CRUD.HOOK.beforeToEdit](crud, form) {
      // 清空附件列表
      this.files = []
      this.tempFiles = []
      this.getFiles()
      return true
    },
    handleScore(obj) {
      this.$refs.indexScore.scoreDialog = true
      this.$refs.indexScore.currentAssessmentId = obj.bisZhsqId
      this.$refs.indexScore.fetchScoreData()
      this.$refs.indexScore.getQzUser()
    },
    async handleConclusion(obj) {
      const zhid = !obj.hasOwnProperty('bisOldId') ? obj.bisZhsqId : obj.bisOldId
      console.log('zhid:', zhid)

      await crudZhsq.findZhsqById({ zhid: zhid }).then(res => {
        console.log('findZhsqById:', res)
        if (!res.hasOwnProperty('lrsj')) {
          this.$notify({
            title: '2025年以后才有评估结论',
            type: 'error',
            duration: 2500
          })
          return
        }
        if (res.lrsj === '') {
          this.$notify({
            title: '2025年以后才有评估结论',
            type: 'error',
            duration: 2500
          })
          return
        }
        const lrsj = moment(res.lrsj).format('YYYYMMDD')
        if (lrsj <= 20241231) {
          this.$notify({
            title: '2025年以后才有评估结论',
            type: 'error',
            duration: 2500
          })
          return
        }
        console.log(lrsj)
        crudZhsq.getFpTargetDate({ zhid: zhid }).then(res3 => {
          console.log('getFpTargetDate:', res3)
          if (res3 === '') {
            this.$notify({
              title: '评估日期丢了',
              type: 'error',
              duration: 2500
            })
            return
          } else {
            this.$refs.indexConclusion.assessmentDate = res3
            if (res.bisZhsqZhlb === '01') {
              this.$refs.indexConclusion.applicantName = res.hljg.bisGsglQymc
              if (res.bisZhsqHjSndj === '0' || res.bisZhsqHjSndj === '1') {
                this.$refs.indexConclusion.isEligible = false
              } else {
                this.$refs.indexConclusion.isEligible = true
              }
            } else {
              this.$refs.indexConclusion.applicantName = res.bisZhsqJhrxm
              if (res.bisZhsqHjSndj === '0' || res.bisZhsqHjSndj === '1' || res.bisZhsqHjSndj === '2') {
                this.$refs.indexConclusion.isEligible = false
              } else {
                this.$refs.indexConclusion.isEligible = true
              }
            }
            this.$refs.indexConclusion.conclusionDialog = true
            this.$refs.indexConclusion.evltName = res.bisZhsqSnryXm
            this.$refs.indexConclusion.idNumber = res.bisZhsqSnrySfz
            this.$refs.indexConclusion.assessmentLevel = res.bisZhsqHjSndj
            this.$refs.indexConclusion.sndj = res.bisZhsqSndj
          }
        })
      })
    },
    handleClick(obj) {
      this.view = 1
      this.crud.toEdit(obj)
    },
    handleEdit(obj) {
      this.view = 0
      this.crud.toEdit(obj)
    },
    /**
     * 查询医保人员
     */
    zhHistoryQuery() {
      this.dialogYjFormVisible = true
      console.log(this.zhHistoryData.length === 0)
      this.zhHistoryDataloading = true
      crudZhsq.getZhHistory({ zhsqId: this.form.bisZhsqId, sort: 'lrsj,desc' }).then(res => {
        console.log(res)
        this.zhHistoryData = res.content
        this.zhHistoryDataloading = false
      }).catch(err => {
        this.zhHistoryDataloading = false
        console.log(err.data.message)
      })
    },
    indexMethod(index) {
      return index + 1
    },
    /**
     * 自定义保存
     * @param zt
     */
    doSave() {
      const titleMsg = '保存成功'
      this.crud.findVM('form').$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.crud.status.edit = CRUD.STATUS.PROCESSING
        crudZhsq.onlyEdit(this.form)
          .then(res => {
            this.crud.status.edit = CRUD.STATUS.NORMAL
            this.$notify({
              title: titleMsg,
              type: 'success',
              duration: 2500
            })
            this.crud.toQuery()
          })
          .catch(err => {
            this.crud.status.edit = CRUD.STATUS.PREPARED
            console.log(err)
          })
      })
    },
    /**
     * 修改操作时，需要加载附件信息
     */
    getFiles: function() {
      crudFile.queryFileByFjlb2(this.form.bisZhsqId).then(res => {
        this.tempFiles = res
        for (let i = 0; i < res.length; i++) {
          this.files.push(res[i].path2)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
